package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.ContextUtils;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.views.SecurityCodeView;

import java.text.DecimalFormat;

import butterknife.BindView;
import retrofit2.Call;

public class IdentityVeriSureActivity extends BaseActivity implements SecurityCodeView.InputCompleteListener{

    private Context context = this;
    private String phone;
    @BindView(R.id.phoneTxt)
    TextView phoneTxt;
    @BindView(R.id.yzmTxt)
    TextView yzmTxt;

    @BindView(R.id.edit_security_code)
    SecurityCodeView edit_security_code;

    private final int totalCount = 60;
    private int time = 1;
    private boolean isClick = true;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_identity_veri_sure;
    }

    @Override
    protected void initView() {
        phone = getIntent().getStringExtra("phone");
        phoneTxt.setText("已发送短信验证码至"+ContextUtils.settingPhone(phone));
        time = 1;
        startCount();
        yzmTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isClick){
                    ToastUtils.toastShort("请稍后再试！");
                    return;
                }
                userscode();
            }
        });
        edit_security_code.setInputCompleteListener(this);
    }

    private void startCount() {
        getWindow().getDecorView().postDelayed(runnable, 1000);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            yzmTxt.setText("接受短信大约需要"+String.valueOf(String.valueOf(totalCount - time))+"秒");
            time++;
            if (time > totalCount) {
                next();
            } else {
                startCount();
            }
        }
    };

    private void next() {
        yzmTxt.setText("点击重新获取");
    }

    @Override
    public void inputComplete() {
        verifyPhone(edit_security_code.getEditContent());
    }

    @Override
    public void deleteContent(boolean isDelete) {
        if (isDelete){
            ToastUtils.toastShort("输入验证码");
        }
    }

    private void verifyPhone(String numStr) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().verifyPhone(numStr, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    launch(IdentityVeriChangeActivity.class);
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void userscode() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryAuth(phone);
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    time = 1;
                    startCount();
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
